package web

import (
	"ksd-social-api/commons/base/controller"
	"ksd-social-api/modules/note/model/context"
	"ksd-social-api/modules/note/service"
	uservice "ksd-social-api/modules/user/service"
	"ksd-social-api/utils"
	"strconv"
)

type NoteChapterController struct {
	controller.BaseController
}

var noteChapterService = service.NoteChapterService{}
var noteStudyUserRecordsService = service.NoteStudyUserRecordsService{}
var userService = uservice.UserService{}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 根据noteId查找
 */
func (web *NoteChapterController) FindChapterLessonsStatus() {
	// 1: 获取小册id
	noteId, _ := web.ParseParamUint64("id")
	// 2: 系统ID
	systemId := web.GetSystemId()
	// 3: 查看小册章节
	chapterList, _ := noteChapterService.FindChapterLessonsStatus(noteId, systemId)
	// 4： 返回
	web.Ok(chapterList)
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 根据noteId查找
 */
func (web *NoteChapterController) FindChapterLessons() {
	// 1: 获取小册id
	noteId, _ := web.ParseParamUint64("id")
	// 2: 系统ID
	systemId := web.GetSystemId()
	if !web.CheckPersmission(noteId, systemId) {
		web.FailCodeMsg(70001, "非法操作！你并非管理员或者老师身份!")
		return
	}
	chapterList, _ := noteChapterService.FindChapterLessons(noteId, systemId)
	web.Ok(chapterList)
}

// 复制章
func (web *NoteChapterController) CopyData() {
	// 1: 获取小册id
	chapterId, _ := web.ParseParamUint64("id")
	noteId, _ := web.ParseParamUint64("noteId")
	// 2: 系统ID
	systemId := web.GetSystemId()
	if !web.CheckPersmission(noteId, systemId) {
		web.FailCodeMsg(70001, "非法操作！你并非作者本身!")
		return
	}
	// 判断用户是否是作者本身，或者是管理员
	noteChapterVo, _ := noteChapterService.CopyChapter(chapterId, systemId)
	web.Ok(noteChapterVo)
}

// 查看章明细---带验证
func (web *NoteChapterController) GetByIdValidator() {
	// 1: 定义一个接受分页信息的上下文
	noteUserRecordsContext := context.NoteUserRecordsContext{}
	// 2: 开始绑定参数
	web.BindJSON(&noteUserRecordsContext)
	// 2: 系统ID
	systemId := web.GetSystemId()
	// 判断用户是否是作者本身，或者是管理员
	noteChapter, _ := noteChapterService.GetById(noteUserRecordsContext.Id.Uint64(), systemId)
	noteChapter.Mdcontent = ""
	// 如果章是收费的，就要开始验证用户是否购买此小册
	if noteChapter.FreeFlag == 1 {
		_, err := noteService.ValidatorUserNoteMain(nil, web.GetUserId(), noteUserRecordsContext.NoteId.Uint64(), systemId)
		if err != nil {
			noteChapter.Htmlcontent = utils.HtmlParseSubstr(noteChapter.Htmlcontent, 1000)
			web.FailCodeMsgData(70001, err.Error(), noteChapter)
			return
		}
	}

	web.Ok(noteChapter)
}

// 查看章明细
func (web *NoteChapterController) SaveUserChapterLessonRecords() {
	// 1: 定义一个接受分页信息的上下文
	noteUserRecordsContext := context.NoteUserRecordsContext{}
	// 2: 开始绑定参数
	web.BindJSON(&noteUserRecordsContext)
	userId := web.GetUserId()
	if userId > 0 && noteUserRecordsContext.ChapterId.Uint64() > 0 {
		// 保存学习记录
		studyUserRecordsContext := context.NoteStudyUserRecordsContext{}
		studyUserRecordsContext.UserId = web.GetUserId()
		studyUserRecordsContext.Uuid = web.GetUuid()
		studyUserRecordsContext.Avatar = web.GetUserAvatar()
		studyUserRecordsContext.Nickname = web.GetUserNickname()
		studyUserRecordsContext.Username = web.GetUsername()
		studyUserRecordsContext.SystemId = web.GetSystemId()
		studyUserRecordsContext.SystemName = web.GetSystemName()
		studyUserRecordsContext.NoteId = noteUserRecordsContext.NoteId.Uint64()
		studyUserRecordsContext.SpecialId = noteUserRecordsContext.SpecialId.Uint64()
		studyUserRecordsContext.ChapterId = noteUserRecordsContext.ChapterId.Uint64()
		if noteUserRecordsContext.LessonId.Uint64() > 0 {
			studyUserRecordsContext.LessonId = noteUserRecordsContext.LessonId.Uint64()
			noteStudyUserRecordsService.SaveNoteStudyUserRecordsLesson(&studyUserRecordsContext)
		} else {
			noteStudyUserRecordsService.SaveNoteStudyUserRecordsChapter(&studyUserRecordsContext)
		}

	}
}

func (web *NoteChapterController) GetById() {
	// 1: 获取小册id
	chapterId, _ := web.ParseParamUint64("id")
	noteId, _ := web.ParseParamUint64("noteId")
	// 2: 系统ID
	systemId := web.GetSystemId()
	if !web.CheckPersmission(noteId, systemId) {
		web.FailCodeMsg(70001, "非法操作！你并非作者本身!")
		return
	}
	// 判断用户是否是作者本身，或者是管理员
	noteChapter, _ := noteChapterService.GetById(chapterId, systemId)
	web.Ok(noteChapter)
}

// 删除章
func (web *NoteChapterController) RemoveByID() {
	// 1: 定义一个接受分页信息的上下文
	noteChapterRemoveContext := context.NoteChapterRemoveContext{}
	// 2: 开始绑定参数
	web.BindJSON(&noteChapterRemoveContext)
	noteChapterRemoveContext.SystemId = web.GetSystemId()
	noteId, _ := strconv.ParseUint(noteChapterRemoveContext.NoteId, 10, 64)
	if !web.CheckPersmission(noteId, noteChapterRemoveContext.SystemId) {
		web.FailCodeMsg(70001, "非法操作！你并非作者本身!")
		return
	}
	// 3：保存
	noteChapterService.RemoveByID(noteChapterRemoveContext)
	// 4: 返回
	web.Ok("success")
}

// 状态控制章
func (web *NoteChapterController) UpdateStatus() {
	// 1: 定义一个接受分页信息的上下文
	noteChapterStatusContext := context.NoteChapterStatusContext{}
	// 2: 开始绑定参数
	web.BindJSON(&noteChapterStatusContext)
	noteId, _ := strconv.ParseUint(noteChapterStatusContext.NoteId, 10, 64)
	if !web.CheckPersmission(noteId, web.GetSystemId()) {
		web.FailCodeMsg(70001, "非法操作！你并非作者本身!")
		return
	}
	// 3：保存
	noteChapterService.UpdateById(noteChapterStatusContext)
	// 4: 返回
	web.Ok("success")
}

// 状态控制章
func (web *NoteChapterController) UpdateContent() {
	// 1: 定义一个接受分页信息的上下文
	noteChapterContentContext := context.NoteChapterContentContext{}
	// 2: 开始绑定参数
	web.BindJSON(&noteChapterContentContext)
	noteChapterContentContext.SystemId = web.GetSystemId()
	noteId, _ := strconv.ParseUint(noteChapterContentContext.NoteId, 10, 64)
	if !web.CheckPersmission(noteId, noteChapterContentContext.SystemId) {
		web.FailCodeMsg(70001, "非法操作！你并非作者本身!")
		return
	}

	if len(noteChapterContentContext.Mdcontent) == 0 {
		web.FailCodeMsg(70001, "请输入内容")
		return
	}

	// 3：保存
	noteChapterService.UpdateByIdContent(noteChapterContentContext)
	// 4: 返回
	web.Ok("success")
}

// 保存修改章
func (web *NoteChapterController) SaveChapter() {
	// 1: 定义一个接受分页信息的上下文
	noteChapterContext := context.NoteChapterContext{}
	// 2: 开始绑定参数
	web.BindJSON(&noteChapterContext)
	noteChapterContext.SystemId = web.GetSystemId()
	noteChapterContext.SystemName = web.GetSystemName()
	noteId, _ := strconv.ParseUint(noteChapterContext.NoteId, 10, 64)
	if !web.CheckPersmission(noteId, noteChapterContext.SystemId) {
		web.FailCodeMsg(70001, "非法操作！你并非作者本身!")
		return
	}

	if len(noteChapterContext.Mdcontent) == 0 {
		web.FailCodeMsg(70001, "请输入内容")
		return
	}

	// 3：保存
	vo, _ := noteChapterService.SaveChapter(noteChapterContext)
	// 4: 返回
	web.Ok(vo)
}

func (web *NoteChapterController) CheckPersmission(noteId uint64, systemId uint64) bool {
	// 判断用户是否是作者本身，或者是管理员
	noteDetailVo, _ := noteService.GetNoteByIDToPay(noteId, systemId)
	// 2: 用户id从哪里获取
	userId := web.GetUserId()
	userVo := userService.GetUserById(userId, systemId)
	return userVo.Role == "admin" || userVo.TeacherFlag == 1 || noteDetailVo.Userid == userId
}
